<?php
/**
 * Created by PhpStorm.
 * User: shaowei
 * Date: 2019/5/9
 * Time: 下午3:24
 */

namespace App\Http\Model;


use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use App\Http\Services\UserFilterServices;

class UserReverify extends Base
{
    protected $table = 'user_reverify';

    public function __construct(array $attributes = []) {
        parent::__construct($this->table, $attributes);
    }


    public function add($userId, $token, $bizNo, $idcardName, $idcardNumber, $guid){

        $data = [
            'user_id' => $userId,
            'token' => $token,
            'biz_no' => $bizNo,
            'idcard_name' => $idcardName,
            'idcard_number' => $idcardNumber,
            'guid' => $guid,
        ];
        DB::table($this->table)->insert($data);
    }

    public function setResult($token, $resultCode, $resultMessage){
        $record = $this->getInfoByUser(['token' => $token]);
        if (!$record) {
            return;
        }

        DB::table($this->table)
            ->where('token', '=', $token)
            ->limit(1)
            ->update([
                'result_code' => $resultCode,
                'result_message' => $resultMessage,
            ]);
    }


    public function get($userId, $token){
        $row = DB::table($this->table)
            ->where('token', '=', $token)
            ->where('user_id', '=', $userId)
            ->first();
        return $row;
    }


    public function getLatest($userId){
        $row = DB::table($this->table)
            ->where('result_code', '=', '1000')
            ->where('user_id', '=', $userId)
            ->orderBy('id', 'desc')
            ->first();
        if (empty($row)){
            return null;
        }
        return ['guid'=>$row->guid,'idcard_name'=>$row->idcard_name, 'idcard_number'=>$row->idcard_number];
    }

    public function getInfoByUser($params){
        $row = DB::table('user_reverify')->where($params)->first();
        $row = self::objToArray($row);
        return $row;
    }
}
